<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="css/eui.css">
</head>
<body>
<div id="app">
    <el-upload
            action="/upload"
            name="picFile"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <form action="">
        <input type="text" name="content" v-model="weibo.content" placeholder="说点儿什么...">
        <input type="button" value="发布微博" @click="send()">
    </form>
</div>
</body>
<script src="js/vue.js"></script>
<!-- import JavaScript -->
<script src="js/eui.js"></script>
<script src="js/axios.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo:{
                    content:"",
                    urls:""
                }
            };
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList);
                axios.get("/remove?name=" + file.response).then(function (response) {
                    console.log("删除完成!");
                })
                //遍历fileList数组取出数组file对象的图片路径装进一个新的数组里面
                let arr = [];
                for (let file of fileList) {
                    //push往数组中添加元素 file.response图片路径
                    arr.push(file.response);
                }
                //把数组中多个图片路径 合并成一个字符串赋值给urls
                v.weibo.urls = arr.toString();

            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response,file, fileList) {
                //遍历fileList数组取出数组file对象的图片路径装进一个新的数组里面
                let arr = [];
                for (let file of fileList) {
                    //push往数组中添加元素 file.response图片路径
                    arr.push(file.response);
                }
                //把数组中多个图片路径 合并成一个字符串赋值给urls
                v.weibo.urls = arr.toString();
            },
            send(){
                //发请求 发布微博
                axios.post("/insert",v.weibo).then(function (response) {
                    if (response.data==1){
                        alert("发布微博成功!");
                        location.href="/";//回到首页
                    }else{
                        alert("请先登录!");
                        location.href = "/login.html";
                    }

                })
            }
        }
    })
</script>
</html>